package com.simple.utils;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.google.common.base.Joiner;
import com.simple.entity.SysMenu;
import com.simple.entity.SysUser;
import com.simple.service.SysMenuService;
import com.simple.service.SysRoleService;
import com.simple.service.SysUserService;

/**
 * 系统配置
 * */
@Component
@DependsOn("springUtils")
public class SysConfig {
	
	 private static final Logger logger = LoggerFactory.getLogger(SysConfig.class);
	
	 public static final String USER_SESSION_KEY ="userSessionKey";
	 public static final String USER_SESSION ="userSession";
	 public static final String USER_SESSION_ID ="userSessionId";
	 
	 public static final String MENU_LIST="menuList";
	 
	 private static SysMenuService sysMenuService = (SysMenuService) SpringUtils.getBean(SysMenuService.class);
	 private static SysRoleService sysRoleService = (SysRoleService) SpringUtils.getBean(SysRoleService.class);
	 private static SysUserService sysUserService = (SysUserService) SpringUtils.getBean(SysUserService.class);
	 
	 private static RedisUtils redisUtils = (RedisUtils) SpringUtils.getBean(RedisUtils.class);
	 
	 /**
	 * 树形结构赋值
	 * 截取二级栏目ID
	 * */
	public static String getSecondLevel(String menuId) {
		List<Object> list=sysMenuService.findSecondLevel(MyStringUtils.StringArrayToInt(menuId));
		String secondMenu = Joiner.on(",").join(list); 
		logger.info("-->得到的二级栏目"+secondMenu);
		return secondMenu;
	}
	//获取缓存栏目
	public static String getMenuCache() {
		String result="";
		System.out.println("redisUtil"+redisUtils);
		Object menuList=redisUtils.getString(MENU_LIST+"_"+getUsername());
		if(menuList!=null) {
			logger.info("menuList"+menuList);
			result=menuList.toString();
		}
		return result;
	}
	
	//删除栏目缓存
	public static void removeMenuCache() {
		 String username=getUsername();
		 boolean flag= redisUtils.delete(MENU_LIST+"_"+username);
		 if(flag) {
			 logger.info("-->删除menuList成功");
		 }else {
			 logger.info("-->删除menuList失败");
		 }
	}
	
	//获取当前用户
	public static String getUsername() {
		Subject subject=SecurityUtils.getSubject();
		Session session=subject.getSession();
		return (String) session.getAttribute(USER_SESSION_KEY);
	}
	//获取当前用户ID
	public static SysUser getUser() {
		Subject subject=SecurityUtils.getSubject();
		Session session=subject.getSession();
		String username=(String) session.getAttribute(USER_SESSION_KEY);
		SysUser sysUser=sysUserService.selectUserName(username);
		
		return sysUser;
	}
	//判断管理层
	public static boolean isManager(SysUser sysUser) {
		Subject subject=SecurityUtils.getSubject();
		if(subject.hasRole("管理员")) {
			return true;
		}
		return false;
	}
	
	//左侧栏目 数据
	public static StringBuffer getSysMenu() {
		String ids=sysRoleService.findMenuByUserName(SysConfig.getUsername());
		StringBuffer element=new StringBuffer();
		if(StringUtils.isNotBlank(ids)){
		//String ids=sysRoleService.findMenuByUserName("root");
			List<SysMenu> sysMenuList=sysMenuService.findByMenuIdsGroup(ids.split(","));
			for (int i = 0; i < sysMenuList.size(); i++) {
				SysMenu menu=sysMenuList.get(i);
				if(menu.getMenuLevel()==3) {
					SysMenu parentMenu=sysMenuService.findByMenuId(menu.getParentId());
					element.append("<li class=\"layui-nav-item\"> <a href=\"javascript:;\"><i class='layui-icon "+parentMenu.getMenuIcon()+"'>  </i>"+parentMenu.getMenuName()+"</a><dl class=\"layui-nav-child\">");
					List<SysMenu> list=sysMenuService.findByInParentId(ids.split(","), parentMenu.getId());
					if(list.size()>0) {
						for (int j = 0; j < list.size(); j++) {
							SysMenu childmenu=list.get(j);
							if(childmenu.getIsBlank()==1) {
								element.append("<dd><a target='_blank' href="+childmenu.getMenuAddress()+"><i class='layui-icon "+childmenu.getMenuIcon()+"'>  </i>"+childmenu.getMenuName()+"</a></dd>");
							}else {
								element.append("<dd><a href="+childmenu.getMenuAddress()+"><i class='layui-icon "+childmenu.getMenuIcon()+"'>  </i>"+childmenu.getMenuName()+"</a></dd>");
							}
						}
					}	
					element.append("</dl></li>");
				}
			}
			//存入缓存
			redisUtils.setValue(MENU_LIST+"_"+SysConfig.getUsername(), element.toString());
			logger.info("HTML拼接结果"+element.toString());
		}
		return element;
	}

	
}
